package com.ecar.apm.model;

import java.util.Date;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ecar.apm.util.GuidGenerator;
import com.github.pagehelper.StringUtil;


public class HttpRequest {
	private static final long serialVersionUID = 1826152029135090793L;
	  protected static final Logger LOGGER = LoggerFactory.getLogger(HttpRequest.class);
	  private int id;
	  private String guid;
	  private String pguid;
	  
	  public static HttpRequest getHttpRequest(HttpRequestForm httpRequestForm)
	  {
	    HttpRequest httpRequest = new HttpRequest();
	    httpRequest.setGuid(httpRequestForm.getGuid());
	    httpRequest.setPguid(httpRequestForm.getPguid());
	    httpRequest.setHttpMethod(httpRequestForm.getHttpMethod());
	    httpRequest.setUrl(httpRequestForm.getUrl());
	    httpRequest.setHeaders(httpRequestForm.getHeaders());
	    httpRequest.setParameters(httpRequestForm.getParameters());
	    httpRequest.setConditionType(httpRequestForm.getConditionType());
	    httpRequest.setCondition(httpRequestForm.getCondition());
	    httpRequest.setResultType(httpRequestForm.getResultType());
	    httpRequest.setVariables(httpRequestForm.getVariables());
	    httpRequest.setMaxConnectionSeconds(httpRequestForm.getMaxConnectionSeconds());
	    return httpRequest;
	  }
	  
	  public enum CheckCondition {
			CONTAINS, DOESNT_CONTAIN, STATUSCODE, DEFAULT
		}
		public enum HttpMethod {
			GET, HEAD, POST, PUT, DELETE
		}
		
		public enum ResultType {
			XML, JSON
		}    
	  
	  private int sort = 1;
	  private String url;
	  private String remark;
	  private CheckCondition conditionType = CheckCondition.DEFAULT;
	  private String condition;
	  private ResultType resultType;
	  private HttpMethod httpMethod;
	  private String headers;
	  private String parameters;
	  private String variables;
	  private HashMap<String, String> headersMap = new HashMap();
	  private HashMap<String, String> parametersMap = new HashMap();
	  private HashMap<String, String> variablesMap = new HashMap();
	  private int maxConnectionSeconds = 10;
	  private Date createTime;
	  private boolean archived;
	  
	  public int getId()
	  {
	    return this.id;
	  }
	  
	  public void setId(int id)
	  {
	    this.id = id;
	  }
	  
	  public String getGuid()
	  {
	    return this.guid;
	  }
	  
	  public void setGuid(String guid)
	  {
	    this.guid = guid;
	  }
	  
	  public String getPguid()
	  {
	    return this.pguid;
	  }
	  
	  public void setPguid(String pguid)
	  {
	    this.pguid = pguid;
	  }
	  
	  public int getSort()
	  {
	    return this.sort;
	  }
	  
	  public void setSort(int sort)
	  {
	    this.sort = sort;
	  }
	  
	  public String getUrl()
	  {
	    return this.url;
	  }
	  
	  public void setUrl(String url)
	  {
	    this.url = url;
	  }
	  
	  public String getRemark()
	  {
	    return this.remark;
	  }
	  
	  public void setRemark(String remark)
	  {
	    this.remark = remark;
	  }
	  
	  public CheckCondition  getConditionType()
	  {
	    return this.conditionType;
	  }
	  
	  public void setConditionType(CheckCondition  conditionType)
	  {
	    this.conditionType = conditionType;
	  }
	  
	  public ResultType getResultType()
	  {
	    return this.resultType;
	  }
	  
	  public void setResultType(ResultType resultType)
	  {
	    this.resultType = resultType;
	  }
	  
	  public HttpMethod  getHttpMethod()
	  {
	    return this.httpMethod;
	  }
	  
	  public void setHttpMethod(HttpMethod  httpMethod)
	  {
	    this.httpMethod = httpMethod;
	  }
	  
	  public String getHeaders()
	  {
	    return this.headers;
	  }
	  
	  public void setHeaders(String headers)
	  {
	    this.headers = headers;
	    this.headersMap = stringToMap(headers);
	  }
	  
	  public String getParameters()
	  {
	    return this.parameters;
	  }
	  
	  public void setParameters(String parameters)
	  {
	    this.parameters = parameters;
	    this.parametersMap = stringToMap(parameters);
	  }
	  
	  public String getVariables()
	  {
	    return this.variables;
	  }
	  
	  public void setVariables(String variables)
	  {
	    this.variables = variables;
	    this.variablesMap = stringToMap(variables);
	  }
	  
	  public HashMap<String, String> getHeadersMap()
	  {
	    return this.headersMap;
	  }
	  
	  public void setHeadersMap(HashMap<String, String> headers)
	  {
	    this.headersMap = headers;
	    this.headers = mapToString(this.headersMap);
	  }
	  
	  public HashMap<String, String> getParametersMap()
	  {
	    return this.parametersMap;
	  }
	  
	  public void setParametersMap(HashMap<String, String> parameters)
	  {
	    this.parametersMap = parameters;
	    this.parameters = mapToString(this.parametersMap);
	  }
	  
	  public HashMap<String, String> getVariablesMap()
	  {
	    return this.variablesMap;
	  }
	  
	  public void setVariablesMap(HashMap<String, String> variables)
	  {
	    this.variablesMap = variables;
	    this.variables = mapToString(this.variablesMap);
	  }
	  
	  public int getMaxConnectionSeconds()
	  {
	    return this.maxConnectionSeconds;
	  }
	  
	  public void setMaxConnectionSeconds(int maxConnectionSeconds)
	  {
	    this.maxConnectionSeconds = maxConnectionSeconds;
	  }
	  
	  public String getCondition()
	  {
	    return this.condition;
	  }
	  
	  public void setCondition(String condition)
	  {
	    this.condition = condition;
	  }
	  
	  public Date getCreateTime()
	  {
	    return this.createTime;
	  }
	  
	  public void setCreateTime(Date createTime)
	  {
	    this.createTime = createTime;
	  }
	  
	  public boolean isArchived()
	  {
	    return this.archived;
	  }
	  
	  public void setArchived(boolean archived)
	  {
	    this.archived = archived;
	  }
	  
	  public String mapToString(HashMap<String, String> map)
	  {
	    if ((map == null) || (map.size() == 0)) {
	      return null;
	    }
	    StringBuffer sb = new StringBuffer();
	    for (String key : map.keySet())
	    {
	      if (sb.length() != 0) {
	        sb.append("\r\n");
	      }
	      sb.append(key).append("::").append((String)map.get(key));
	    }
	    return sb.toString();
	  }
	  
	  public HashMap<String, String> stringToMap(String text)
	  {
	    if (StringUtil.isEmpty(text)) {
	      return null;
	    }
	    HashMap<String, String> map = new HashMap();
	    String[] strArray = text.split("\r\n");
	    for (String str : strArray)
	    {
	      String[] header = str.split("::");
	      
	      map.put(header[0], header[1]);
	    }
	    return map;
	  }
}